package com.gsnd.briuppts.web.controller;

import com.gsnd.briuppts.entity.BaseAccount;
import com.gsnd.briuppts.entity.Do.BaseAccountExtend;
import com.gsnd.briuppts.entity.Do.LoginVo;
import com.gsnd.briuppts.Result.Response;
import com.gsnd.briuppts.service.BaseAccountService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController

public class BaseAccountController {

    // 调用业务层的代码
    @Autowired
    BaseAccountService baseAccountService;

    @PostMapping("/login")
    public Response<LoginVo> login(String username,
                                   String password){
        LoginVo loginVo = baseAccountService.login(username, password);
        // 登陆的结果，返回携带了用户id和用户name的token
        return Response.success(loginVo);
    }

    @GetMapping("/me")
    public Response<BaseAccountExtend> findCurrentAccountInfo(HttpSession session){
        String accountId = (String)session.getAttribute("userId");
        BaseAccountExtend account = baseAccountService.findAccountAndRoleByAccountId(accountId);
        return Response.success(account);
    }

    @GetMapping("/logout")
    public Response logout(){
        return Response.success();

    }
}
